/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.meteocal.control;

import it.polimi.meteocal.entity.*;
import it.polimi.meteocal.entity.User;
import javax.ejb.EJB;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.UserTransaction;
import javax.validation.ConstraintViolationException;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.*;
import org.junit.runner.RunWith;

/**
 *
 * @author miglie
 */
@RunWith(Arquillian.class)
public class UserManagerIT {

    @EJB
    UserManager cut;

    @PersistenceContext
    EntityManager em;

    @BeforeClass
    public static void setUpClass() {

    }

    @Deployment
    public static WebArchive createArchiveAndDeploy() {
        return ShrinkWrap.create(WebArchive.class)
                .addClass(UserManager.class)
                .addClass(User.class)
                .addClass(InvitedList.class)
                .addClasses(InvitedListPK.class, MessageList.class, Message.class, MessageListPK.class, Forecast.class, Event.class)
                .addPackage(User.class.getPackage())
                .addAsResource("test-persistence.xml", "META-INF/persistence.xml")
                .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
    }

    @Test
    public void UserManagerShouldBeInjected() {
        assertNotNull(cut);
    }

    @Test
    public void EntityManagerShouldBeInjected() {
        assertNotNull(em);
    }

    @Test
    public void newUsersShouldBeValid() {
        System.out.println("New User Shold Be Valid Test \n\n\n\n");
        User newUser = new User();
        newUser.setEmail("invalidemail");
        newUser.setName("a");
        newUser.setSurname("a");
        newUser.setPassword("a");
        newUser.setPublicCalendar(Boolean.TRUE);
        newUser.setGroupname("USER");
        System.out.println(cut);
        try {
            cut.save(newUser);
            System.out.println("NON DOVREBBE ESSERE STAMPATO");
        } catch (Exception e) {
            System.out.println("CATCH ERRORE");
            System.out.println(e.getCause());
            assertTrue(e.getCause() instanceof ConstraintViolationException);
            
        }
    }
}
